package com.zyx.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyx.mapper.AutoMapper;
import com.zyx.pojo.Auto;
import com.zyx.pojo.AutoExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AutoService {
    @Resource
    private AutoMapper autoMapper;


    /**
     * 查询车辆数目和每日新增
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public Map<String,Integer> queryAllCar(){

        AutoExample example = new AutoExample();
        int l = autoMapper.countByExample(example);
        int i = autoMapper.queryByTime();
        Map<String,Integer> map = new HashMap<>();
        map.put("data_size",l);
        map.put("data_new",i);

        return map;
    }

    /**
     * 根据车辆部分信息查询数据
     * @author zyx
     * @date 19:26 2022/2/11
     * @name queryAll
     * @param auto
     * @return java.util.List<com.zyx.pojo.Auto>
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Auto> queryAuto(Auto auto){
        if (auto.getAutoName().equals("")){
            auto.setAutoName(null);
        }
        return autoMapper.selectBy(auto);
    }

    /**
     * 分页模糊查询和查询所有数据
     * @param pageNum
     * @param pageSize
     * @param auto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public PageInfo<Auto> queryAll(Integer pageNum, Integer pageSize,Auto auto){
        AutoExample example = new AutoExample();
        AutoExample.Criteria criteria = example.createCriteria();
        if (auto != null){
            //根据车辆名称进行模糊查询
            if (auto.getAutoName() != null && !"".equals(auto.getAutoName().trim())){
                System.out.println(auto.getAutoName().trim());
                criteria.andAutoNameLike("%"+auto.getAutoName().trim()+"%");
            }
            //根据汽车价格进行模糊查询
            if (auto.getPrice() != null && !"".equals(auto.getPrice())){
                criteria.andPriceLessThanOrEqualTo(auto.getPrice());
            }
            //根据汽车品牌进行模糊查询
            if (auto.getFirm() != null && !"".equals(auto.getFirm().trim())){
                criteria.andFirmLike("%"+auto.getFirm()+"%");
            }
            //根据车辆大小进行模糊查询
            if (auto.getRan() != null && !"".equals(auto.getRan().trim())){
                criteria.andRanEqualTo(auto.getFirm());
            }
            //根据车辆上市时间查询
            if (auto.getTimeMarket() != null){
                criteria.andTimeMarketLessThanOrEqualTo(auto.getTimeMarket());
            }
            //根据车辆动力情况进行模糊查询
            if (auto.getEnergy() != null && !"".equals(auto.getEnergy().trim())){
                criteria.andEnergyEqualTo(auto.getEnergy());
            }
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);//只对紧邻的查询语句有效，是用拦截器的方式实现
        List<Auto> list = autoMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 添加车辆信息
     * @param auto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})//REQUIRED 事务 rollbackFor遇到异常回滚
    public int addAuto(Auto auto){
        return autoMapper.insertSelective(auto);
    }

    /**
     * 根据主键查询信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Auto selectByKey(int id){
        return autoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键删除----逻辑删除：实际开发中一般不允许把数据从磁盘内删除
     * @param auto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteAuto(Auto auto){
        auto.setUsed(3);
        int i = autoMapper.updateByPrimaryKey(auto);
        return i;
    }

    /**
     * 根据主键动态更新数据
     * @param auto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateAuto(Auto auto){
        return autoMapper.updateByPrimaryKeySelective(auto);
    }

    /**
     * 查询信息
     * @param auto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Auto> selectBy(Auto auto){
        return autoMapper.selectBy(auto);
    }

    /**
     * 根据车辆时间查询信息
     * @param date
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Auto selectId(Date date){
        return autoMapper.selectId(date);
    }

    //查询同系列车最大价格
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public int queryMax(String autoName){
        return autoMapper.queryMax(autoName);
    }

    //查询同系列车最小价格
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public int queryMin(String autoName){
        return autoMapper.queryMin(autoName);
    }

    //根据id查询汽车类型有多少图片
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public int queryPicture(int autoId){
        return autoMapper.queryPicture(autoId);
    }

    //根据主键修改
}
